<?php
	Class SQLInterface{

		//Configuration
		
		private $serverType			="Win";
		private $server				;
		private $database			="MESCAN";
		private $userName			="mescan";
		private $password			="mescan";
		private $serverIP			="127.0.0.1";
		//private $serverIP			="192.168.0.100";
		//private $serverIP			="192.168.0.151";
		//private $serverIP			="192.168.14.120";
		//private $serverIP			="192.168.1.3";
		//private $serverIP			="178.79.33.219";
		private $serverPort		="1210";
		//private $serverPort			="9586";


		private $conn;
		private $queryResult = array();
		private $result = array();

	    function __construct(){
	        $argV = func_get_args();
	        $argC = func_num_args();
	        if (method_exists($this,$constructerFuntionName='__construct'.$argC)) {
	            call_user_func_array(array($this,$constructerFuntionName),$argV);
	        }
	    } 
		
		public function __construct0() {
			$this->setupServer();
		}

		public function __construct1($type) {
			$this->serverType = $type;
			$this->setupServer();
		}
    
		private function setupServer(){
			if ($this->serverType == ""){
				$this->serverType = "Win";
			}
			
			if($this->serverType == "Win"){
				$this->server = $this->serverIP."\sqlexpress ,".$this->serverPort; //serverName\instanceName
			}
			if($this->serverType == "Lin"){
				$this->server = $this->serverIP.":".$this->serverPort; //serverName\instanceName
			}
		}
		
		public function query($query){

			if($this->serverType == "Win"){
				$connectionInfo = array( "Database"=>"".$this->database, "UID"=>"".$this->userName, "PWD"=>"".$this->password);
				$this->conn = sqlsrv_connect( $this->server, $connectionInfo);

				if( !$this->conn ) {
					$temp = array();
					$temp['error'] = "Connection could not be established";
					print(json_encode($temp));
				}

				$this->queryResult = sqlsrv_query( $this->conn, $query);
				
			    while ($row = sqlsrv_fetch_array( $this->queryResult, SQLSRV_FETCH_ASSOC)){
			    	array_push($this->result, $row);
			    }
				sqlsrv_close( $this->conn );

			}
			if($this->serverType == "Lin") {
				/* In order to prevent JSON parse error ini_set('mssql.charset', 'UTF-8'); 
				has to be called and configuration in /etc/freetds/freetds.conf 
				to be changed to version 7. 
				Example:
				[global]
	    			# TDS protocol version
	    			tds version = 7.0*/
				ini_set('mssql.charset', 'UTF-8');
				$this->conn = mssql_connect($this->server, $this->userName, $this->password);

				if( !$this->conn ) {
					$temp = array();
					$temp['error'] = "Connection could not be established";
					print(json_encode($temp));
				}

				mssql_select_db($this->database, $this->conn);
				$this->queryResult = mssql_query( $query, $this->conn);
				mssql_close($this->conn);

			    while ($row = mssql_fetch_array($this->queryResult)){
			    	array_push($this->result, $row);
			    }

			    mssql_free_result($this->queryResult);
			}

			return $this->result;
		}
	}	
?>
